<?php
namespace App\Http\Controllers\Client\Talent;

use App\Http\Controllers\ClientController;
use App\Models\Common\Common;
use App\Models\Common\File;
use App\Models\Talent;
use App\Models\Tools\Data;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TalentUploadController extends ClientController
{

    public function index()
    {

        $all = $this->all;

        $work_id = $all['work_id'];
        $submit_img = $all['submit_img'];
        $submit_remark = $all['submit_remark'];

        try{
            DB::beginTransaction();
            // 验证工单状态
            $work = DB::table('explore_work')->where('work_id','=',$work_id)->lockForUpdate()->first();
            $work = json_decode(json_encode($work),true);
            if($work['receiver_we_id'] !== $this->we_id){
                return $this->setReturnJson(-1,'这个工单不属于您');
            }
            if(!in_array($work['status'],[Data::DATA_DB_EXPLORE_WORK_STATUS_INIT, Data::DATA_DB_EXPLORE_WORK_STATUS_REFUSE])){
                return $this->setReturnJson(-1,'当前工单状态为【'.Data::$exploreWorkStatus[$work['status']].'】，不允许投稿');
            }

            // 保存图片
            $file_path = 'images/work/' . $work['eo_id'];
            $submit_img = File::saveMedia($submit_img,$file_path);

            // 修改状态
            DB::table('explore_work')->where('work_id','=',$work_id)->update([
                'status' => Data::DATA_DB_EXPLORE_WORK_STATUS_SUBMIT, // 修改为已投稿状态
                'submit_time' => date('Y-m-d H:i:s'),
                'submit_img' => $submit_img,
                'submit_remark' => $submit_remark,
            ]);
            // 记录操作日志
            DB::table('explore_work_log')->insert([
                'work_id'=>$work_id,
                'action'=>Data::DATA_DB_EXPLORE_WORK_ACTION_SUBMIT,
                'we_id' => $this->we_id,
                'remark' => '用户【'.$this->nickname.'】提交投稿' . ($submit_remark?'，投稿备注：'.$submit_remark:''),
            ]);
            // 发送投稿微信通知
            $shop_name = DB::table('shop')->where('shop_id','=',$work['shop_id'])->value('shop_name');
            $data = [
                'first' => '您在店铺【' . $shop_name . '】发布的探店任务达人已投稿，请及时审核。',
                'keyword1' => '达人探店',
                'keyword2' => date('Y-m-d H:i:s'),
                'remark' => [
                    'value' => '请在48小时内审核投稿，审核超时将自动通过。',
                    'color' => '#F00'
                ],
            ];
            //发送通知
            $openid = DB::table('lottery_users')->where('we_id','=',$work['apply_we_id'])->value('openid');
            Common::sendLaifeiMessage($openid,'contribute',$data,'/history?tab=1');
        }catch (\Exception $e){
            DB::rollBack();
            Log::error(TalentUploadController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'投稿失败');
        }
        DB::commit();
        return $this->setReturnJson(0,'您已投稿成功，工作人员将尽快为您进行审核');

    }

}
